<?php
require 'nette.min.php';
Nette\Diagnostics\Debugger::enable();
Nette\Diagnostics\Debugger::$strictMode = TRUE;
require '../../loader.php';
?>
<html><head>
<link rel="stylesheet" type="text/css" href="/public/wdb.css" />
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="/public/wdb.js"></script>
<script type="text/javascript">
function wdbf_AValidator(form)
{
  if (form.wdbValidation.aValidator == undefined) return true;
  var success = true;     
  for (var key in form.wdbValidation.aValidator)
  {
    var column = form.wdbValidation.columns[form.wdbValidation.aValidator[key]];
    var value = column.element.val();
    if (value != 'aaa')
    {
      form.wdbValidation.eventDispatcher.error(form, column, 'Musi byt rovno "a"!');
      success = false;
    }
    else
    {
      form.wdbValidation.eventDispatcher.success(form, column);
    }
  }
  return success;   
}
wdb.registerValidator('AValidator', wdbf_AValidator);
</script>
</head><body>
<?php
class AValidator implements WDB\Validation\iValidator
{
  private $columnName;
  public function __construct($columnName)
  {
    $this->columnName = $columnName; 
  }
  public function fetchValidatorData
      (WDB\Wrapper\iRecord $record, &$data)
  {
    if (!isset($data['aValidator'])) $data['aValidator'] = array();
    $data['aValidator'][] = $this->columnName; 
  }
  
  public function raise(WDB\Wrapper\iRecord $record = NULL)
  {
    if ($record === NULL) throw new Exception\BadArgument(
      "Validator event raise() method needs WDB\\Wrapper".
      "\\iRecord as a raise argument");
    if ($record[$this->columnName] == 'aaa')
    {
      return true;
    }
    else
    {
      $columns = $record->getTable()->getColumns();
      $column = $columns[$this->columnName];
      $record->getField($this->columnName)->addValidationError
          ('Sloupec '.$column->getTitle().' nema hodnotu "a" ');
      return false;
    }
  }
}

//predpokladejme existenci tabulky foo ve vychozim databazovem
//spojeni obsahujici textovy sloupec s nazvem 'bar'
$table = WDB\Wrapper\TableFactory::fromName('users');
$table->addValidator(new AValidator('name'));

WDB\WebUI\TableFactory::fromWrapper($table)->show();

?>
</body></html>